/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.sesame.domain.Album;
import com.sesame.domain.Notice;
import com.sesame.domain.User;
import com.sesame.service.AdminService;
import com.sesame.service.AlbumService;
import com.sesame.service.UserService;

/**
 * @author voidman
 * 
 */
public class EditNoticeController extends SimpleFormController {

    private AdminService adminService;
    private Integer noticeId;

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {

        noticeId = Integer.parseInt(request.getParameter("noticeId"));
        Notice notice = adminService.showNotice(noticeId);
        return notice;

    }

    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {

        if ("admin".equals(request.getSession().getAttribute("adminRank"))) {
            Notice notice = (Notice) command;
            notice.setId(noticeId);

            Map model = errors.getModel();

            if (adminService.isEditNotice(notice, model)) {
                return new ModelAndView("redirect:noticeList.html");
            } else {
                return new ModelAndView(getFormView(), model);
            }
        } else {
            return new ModelAndView("redirect:adminError.html");
        }
    }

}